package com.huang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huang.mapper.MenuMapper;
import com.huang.pojo.Menu;
import com.huang.pojo.MenuExample;
import com.huang.utils.ToListInt;
import com.huang.vo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class MenuService {
    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询所有menu菜单
     * @return
     */
    public List<Menu> getAll(){
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        // 查找未删除的
        criteria.andIsDelEqualTo(0);
        return menuMapper.selectByExample(menuExample);
    }

    /**
     * 分页+条件模糊查询
     * @param m_name
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageResult list(String m_name, Integer pageIndex, Integer pageSize){
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        if (m_name != null && !"".equals(m_name)){
            criteria.andM_nameLike("%"+m_name+"%");
        }
        // 查找未删除的
        criteria.andIsDelEqualTo(0);
        PageHelper.startPage(pageIndex,pageSize);
        List<Menu> menus = menuMapper.selectByExample(menuExample);
        PageInfo<Menu> info = new PageInfo<>(menus);
        // 封装数据
        PageResult pageResult = new PageResult(menus,
                (int) info.getTotal(),
                info.getPageNum(),
                info.getPages(),
                info.getPrePage(),
                info.getNextPage(),
                1,
                info.getPages(),
                info.getNavigatepageNums());
        return pageResult;
    }

    /**
     * 根据name查找是否有该资源
     * @param name
     * @return 对象信息
     */
    public Menu isExistName(String name){
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andM_nameEqualTo(name);
        List<Menu> menus = menuMapper.selectByExample(menuExample);
        return menus.size()==0?null:menus.get(0);
    }

    /**
     * 添加资源
     * @param menu
     * @return
     */
    public int insert(Menu menu){
        return menuMapper.insert(menu);
    }

    /**
     * 通过id查找菜单资源
     * @param id
     * @return
     */
    public Menu findById(Integer id){
        return menuMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过主键更新信息
     * @param menu
     * @return
     */
    public int update(Menu menu){
        menu.setIsDel(0);
        return menuMapper.updateByPrimaryKey(menu);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        Menu menu = new Menu();
        menu.setM_id(id);
        menu.setIsDel(1);
        int i = menuMapper.updateByPrimaryKeySelective(menu);
        return i;
    }

    /**
     * 逻辑批量删除
     * @param ids 字符串数组
     * @return
     */
    public int delete(String[] ids){
        // 将 String数组转换为 list<Integer>
        List<Integer> list = ToListInt.StringToList(ids);
        int i = menuMapper.deleteByBatch(list);
        return i;
    }
}
